<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use App\Utils\ModelOperatorTrait;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;

class SnapAbnormalExport implements FromQuery, WithHeadings, WithMapping
{
	use ModelOperatorTrait;

    protected $conditions;

	public function __construct($conditions = []){
		$this->conditions = $conditions;
	}

	protected function model(){
		return app(\App\Models\SnapAbnormal::class);
	}

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
    	$snapAbnormal = $this->model();

    	if(isset($this->conditions['vehicle_number'])){
    		$snapAbnormal = $snapAbnormal->where('vehicle_number','=',$this->conditions['vehicle_number']);
    	}

    	if(isset($this->conditions['start_time'])){
    		$snapAbnormal = $snapAbnormal->where('system_date','>=',$this->conditions['start_time']);
    		$snapAbnormal = $snapAbnormal->where('system_date','<=',$this->conditions['end_time']);
    	}

    	$snapAbnormal = $snapAbnormal->orderBy('system_date','DESC');

        return $snapAbnormal;
    }

    public function map($snapAbnormal):array{
    	return [
    		$snapAbnormal->ic_id,
    		$snapAbnormal->vehicle_number,
    		$snapAbnormal->capture_vehicle_number,
    		$snapAbnormal->capture_filename,
    		$snapAbnormal->abnormal_information,
    		$snapAbnormal->handling_situation_desc,
    		$snapAbnormal->order_systemnumber,
    		$snapAbnormal->system_date,
    		$snapAbnormal->remark
    	];
    }

    public function headings():array{
    	return [
    		'IC卡号',
    		'车牌号',
    		'车牌识别',
    		'抓拍图片',
    		'异常信息',
    		'处理情况',
    		'系统时间',
    		'备注'
    	];
    }
}
